#include <Arduino.h>
#include <Arduino_JSON.h>
#include "HttpServer.h"
#include "HttpParser.h"
#include "Configuration.h"

using namespace Rp2040;

HttpResponse handleRequest(HttpRequest request);
HttpServer httpServer(Configuration::getDeviceModel());

void setup()
{
    Serial.begin(115200);
    Serial.println("init started");
    httpServer.init(const_cast<unsigned char *>(Configuration::serverIp));
    Serial.println("init finished");
}

void loop()
{
    httpServer.handleRequest(&handleRequest);
}

HttpResponse handleGetRequest(HttpRequest request)
{
    HttpResponse response;
    response.headers["Connection"] = "close";
    response.headers["Content-Type"] = "text/plain";

    if (request.methodName == "" || request.methodName == "/")
    {
        //bool isAuthorized = request.headers["Authorization"] == "Basic dXNlcjpwYXNz"; // UserName: user, Password: pass
        response.code = 200;
        response.codeDescription = "Success";
        response.body = "hello, world~!\r\nThis is Mozart!\r\n";
    }
    else
    {
        response.code = 404;
        //response.codeDescription = "Method Not Allowed";
        response.codeDescription = "Not Found";
        response.body = "";
    }

    char buffer[12];  // 足够容纳32位无符号整数的最大值(4294967295)
    sprintf(buffer, "%u", response.body.length());  // 注意使用%u而不是%d
    arduino::String lenth(buffer);
    response.headers["Content-Length"] = lenth;
    return response;
}

HttpResponse handleHeadRequest(HttpRequest request)
{
    HttpResponse response;
    response.headers["Connection"] = "close";
    response.code = 200;
    response.codeDescription = "OK";
    return response;
}

HttpResponse handleOtherRequest(HttpRequest request)
{
    HttpResponse response;
    response.headers["Connection"] = "close";
    response.headers["Content-Type"] = "application/json";

    if (request.methodType == POST) {

        if (request.methodName == "api") { // 不带根路径符号
            JSONVar obj;
            obj["msg"] = "OK";
            obj["data"] = request.body;

            response.code = 200;
            response.codeDescription = "OK";
            //response.body = JSON.stringify(obj);
            response.body += "Got " + HttpParser::getRawMethodType(request.methodType) + " method type";
            response.body += "\r\n";
            response.body += "Got " + request.methodName + " method";
            response.body += "\r\n";
            response.body += "Got Body: " + request.body;
        }

        else {
            response.code = 404;
            response.codeDescription = "Not Found";
            response.body = "";
        }

    }

    else {
        response.code = 405;
        response.codeDescription = "Method Not Allowed";
        response.body = "";
    }

    char buffer[12];  // 足够容纳32位无符号整数的最大值(4294967295)
    sprintf(buffer, "%u", response.body.length());  // 注意使用%u而不是%d
    arduino::String lenth(buffer);
    response.headers["Content-Length"] = lenth;
    return response;
}

HttpResponse handleRequest(HttpRequest request)
{
    switch (request.methodType)
    {
    case HttpMethodType::GET:
        return handleGetRequest(request);

    case HttpMethodType::HEAD:
        return handleHeadRequest(request);

    default:
        return handleOtherRequest(request);
    }
}